package com.ruoyi.common.utils.email;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.redis.RedisUtils;

/**
 * 邮箱验证码校验工具类
 */
public class EmailCodeVerifyUtils {

    /**
     * 校验邮箱验证码
     * @param key
     * @param email
     * @param emailCode
     */
    public static void emailCodeValid(String key, String email,String emailCode) {
        long timeToLive = RedisUtils.getTimeToLive(key + email);
        if (timeToLive == -2) {
            throw new ServiceException("验证码已失效");
        }
        String code = Convert.toStr(RedisUtils.getCacheObject(key + email),"");
        if (!code.equals(emailCode)) {
            throw new ServiceException("验证码错误!");
        }
    }
}
